package com.study.thread.waitOrNotfy;

public class WaitNotifyDemo2 {
    public static void main(String[] args) {
        Goods goods = new Goods();


        new Thread(() -> {
            while (true) {
                synchronized (goods) {
                    while (goods.get() > 0) {
                        try {
                            System.out.println("阻塞生产：");
                            goods.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }

                    goods.add();
                    System.out.println(Thread.currentThread().getName() + "执行生产：" + goods.get());
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    goods.notifyAll();
                }
            }
        }, "生产者A").start();
        new Thread(() -> {
            while (true) {
                synchronized (goods) {
                    while (goods.get() > 0) {
                        try {
                            System.out.println("阻塞生产：");
                            goods.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }

                    goods.add();
                    System.out.println(Thread.currentThread().getName() + "执行生产：" + goods.get());
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    goods.notifyAll();
                }
            }
        }, "生产者B").start();

        new Thread(() -> {
            while (true) {
                synchronized (goods) {
                    while (goods.get() <= 0) {
                        try {
                            System.out.println("阻塞消费：");
                            goods.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    goods.decler();
                    System.out.println(Thread.currentThread().getName() + "执行消费：" + goods.get());

                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    goods.notifyAll();
                }
            }
        }, "消费者B").start();
        new Thread(() -> {
            while (true) {
                synchronized (goods) {
                    while (goods.get() <= 0) {
                        try {
                            System.out.println("阻塞消费：");
                            goods.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    goods.decler();
                    System.out.println(Thread.currentThread().getName() + "执行消费：" + goods.get());

                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    goods.notifyAll();
                }
            }
        }, "消费者A").start();
    }
}
